package com.markus.code.exam.xiaomi;

import java.util.Scanner;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/8 18:42
 * todo:
 */
public class Main1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            char[][] board = {
                    {'A','B','C','E'},
                    {'S','F','C','S'},
                    {'A','D','E','E'}
            };
            String word = scanner.next();
            word = word.toUpperCase();
            Main1 demo = new Main1();
            System.out.println(demo.isTrue(board,word));
        }
    }
    private boolean isTrue(char[][] board,String path){
        boolean[][] visited = new boolean[board.length][board[0].length];
        for (int i = 0;i<board.length;i++){
            for (int j=0;j<board[i].length;j++){
                if (process(board,board.length,board[i].length,i,j,path,0,visited)){
                    return true;
                }
            }
        }
        return false;
    }
    private boolean process(char[][] board,int rows,int cols,int row,int col,String path,int index,boolean[][] visited){
        if (row < 0 || row >= rows || col < 0 || col >= cols || visited[row][col]){
            return false;
        }
        if (index == path.length()){
            return true;
        }
        if (board[row][col] != path.charAt(index)){
            return false;
        }
        visited[row][col] = true;
        boolean res =  process(board,rows,cols,row+1,col,path,index+1,visited)||
                process(board,rows,cols,row-1,col,path,index+1,visited)||
                process(board,rows,cols,row,col+1,path,index+1,visited)||
                process(board,rows,cols,row,col-1,path,index+1,visited);
        visited[row][col] = false;
        return res;
    }
}
